<template>
	<view class="box screen-all flex center">
		<view class="bg-view-box flex" @click="wxUse">
			<image class="bg-view flex" src="../../static/scan_page.png"></image>
			<image class="btn-view" src="../../static/scan_btn.png"></image>
		</view>
	</view>
</template>

<script>
	import wxUtil from '../../xlblib/wechat/wx-util.js';
	
	export default {
		data() {
			return {
				
			}
		},
		onLoad(opt) {
			if(opt.tableNumber){				
				this.bindInviteCode(uni.getStorageSync("invite_code"));
				wxUtil.use();
			}else{
				uni.showToast({ title: '初始化...', icon: 'loading', duration: 10000 });
				wxUtil.use().then(()=>{
					// console.log('微信初始化成功');
					this.wxUse();
				}).catch(()=>{
					this.wxUse();
				});
			}
		},
		methods: {
			wxUse () {
				let that = this;
				setTimeout(()=>{
					uni.hideToast();
					wxUtil.use().then(()=>{
						console.log(wxUtil.jweixin);
						wxUtil.jweixin.scanQRCode({
							needResult: 1,
							scanType: ["qrCode"],
							success: function (res) {
								var result = res.resultStr;
								console.log(result);
								//解析数据问题
								if(result.indexOf('=') != -1){
									let _params = result.split('=');
									if(_params.length == 2){
										let _paramList = _params[1].split('_');
										if(_paramList.length == 3){
											let invite_code = _paramList[2];
											// uni.showToast({ title: invite_code, icon: 'none',duration:2000 });
											//缓存桌位号
											uni.setStorageSync("invite_code", invite_code);
											//验证桌位号是否正确
											that.bindInviteCode(invite_code);
											//跳转到扫码点餐页面
											// uni.reLaunch({
											// 	url: "../product-list/goods-list"
											// })
										}else{uni.showToast({ title: '请扫描正确的二维码！', icon: 'none',duration:2000 })}
									}else{uni.showToast({ title: '请扫描正确的二维码！', icon: 'none',duration:2000 })}
								}else{uni.showToast({ title: '请扫描正确的二维码！', icon: 'none',duration:2000 })}
							}
						})
					});
				}, 10);
			},
			bindInviteCode (invite_code) {
				let params = {
					token: uni.getStorageSync("token"),
					invite_code: invite_code,
				}
				console.log(params);
				this.$request("api/user/ceshi", params)
					.then(({ code, msg, data, time }) => {
						console.log('测试绑定桌位号=', code, msg, data);
						if(code == 1){
							//跳转到扫码点餐页面
							uni.reLaunch({
								url: "../product-list/goods-list"
							})
						}else{
							uni.showToast({ title: msg, icon: 'none',duration:2000 })
						}
					}).catch((e) => {
						uni.showToast({ title: '绑定桌位码失败', icon: 'none',duration:2000 })
						console.log(e);
					});
			},
		}
	}
</script>

<style>
	.box {
		background-color: #010B24;
	}
	.bg-view-box {
		width: 634rpx;
		height: 751rpx;
		position: relative;
	}
	.bg-view {
		width: 634rpx;
		height: 751rpx;
		position: absolute;
		top: 0;
	}
	.btn-view {
		width: 263rpx;
		height: 73rpx;
		position: absolute;
		left: 189rpx;
		bottom: 156rpx;
	}
</style>
